<?php
// Registration script //

// Loading standart function library
require_once('lib_load.php');
// Loading Mysql class
load_lib('DbMysql');

// Creation of Mysql class
$db = new DbMysql();

if(isset($_POST['REGSend']))
{
    $errors = '';

    // Checking inputs
    if(!preg_match("/^[a-zA-Z0-9_]+$/", $_POST['REGLogin']))
        $errors .= '<br />Your name contains some forbidden characters.';

    if((strlen($_POST['REGLogin'])<4)
    OR (strlen($_POST['REGLogin'])>20)
    OR (strlen($_POST['REGPass'])<6)
    OR (strlen($_POST['REGPass'])>20))
        $errors .= '<br />Your name/password are too small/large.';

    // Checking the email-input with regexp, later will be changed with "Send a mail"
    if((!preg_match("/^(?:[a-z0-9]+(?:[-_]?[a-z0-9]+)?@[a-z0-9]+(?:\.?[a-z0-9]+)?\.[a-z]{2,5})$/i",$_POST['REGEmail']))
    OR (strlen($_POST['REGEmail'])>126)
    OR (strlen($_POST['REGEmail'])<4))
        $errors .= '<br />You\'ve made a mistake in your e-mail.';

    if($_POST['REGPass'] != $_POST['REGRePass'])
        $errors .= '<br />Passwords aren\'t same.';

    if(($_POST['REGDay']<1)
    OR ($_POST['REGDay']>31)
    OR ($_POST['REGMonth']<1)
    OR ($_POST['REGMonth']>12)
    OR ($_POST['REGYear']<1900)
    OR ($_POST['REGYear']>2006))
        $errors .= '<br />Error in your date, please, recheck it.';

    if ($db->IsExist('users', 'login = \''.$_POST['REGLogin'].'\''))
        $errors .= '<br />Someone has the same login you\'ve mentioned. Please, write another one.';


    if ($db->IsExist('users', 'login = \''.$_POST['REGEmail'].'\''))
        $errors .= '<br />Someone has the same e-mail you\'ve mentioned. Please, write another one.';


    if ($errors == '')
    {
        $db->AddLine('users', array('id' => NULL,
                                    'login'     => mysql_real_escape_string($_POST['REGLogin']),
                                    'password' => md5($_POST['REGPass']),
                                    'email' => mysql_real_escape_string($_POST['REGEmail']),
                                    'sex' => intval($_POST['REGSex']),
                                    'b_day' => intval($_POST['REGDay']),
                                    'b_month' => intval($_POST['REGMonth']),
                                    'b_year' => intval($_POST['REGYear']),
                                    'reg_time' => time()));
        echo 'Your login was successfully registred <a href="login.php">back</a>';
    }
    else
        echo substr($errors, 6);

}
else
{
    // Setting year sequence for output
    $year_seq = '';
    for ($i=date("Y"); $i>=1930; $i--)
        $year_seq .= '<option value="'.$i.'">'.$i.'</option>';
        
    // Loadfing the infomation in main_buffer
    $main_buf = implode('',file('templates/index.htm')); // main tamplate
    $reg_buf = implode('',file('templates/data/registration/index.htm'));
    $reg_buf = str_replace("[%YEAR%]", $year_seq, $reg_buf);
    $main_buf = str_replace("[%INFO%]",$reg_buf ,$main_buf);

    // main output
    echo $main_buf;
}

?>

